﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

namespace ChinaCustoms.Qingdao.Workflow.Controls
{
    /// <summary>
    ///ModalUtil 的摘要说明
    /// </summary>
    public static class ModalUtil
    {
        /// <summary>
        /// 获取关闭模式窗口的JavaScript代码
        /// </summary>   
        /// <param name="page"></param>
        /// <param name="returnValue">是否生成“return false;”代码</param>
        public static void ClosePage(Page page, string returnValue)
        {
            string scriptKey = string.Format("Tunynet_ShowModalDialog_ClosePage_{0}",returnValue);
            if (!page.ClientScript.IsStartupScriptRegistered(scriptKey))
            {
                page.ClientScript.RegisterStartupScript(page.GetType(), scriptKey, GetCloseScript(returnValue));
            }
        }

        /// <summary>
        /// 获取关闭模式窗口的JavaScript代码
        /// </summary>   
        public static void ClosePage(Page page)
        {
            string scriptKey = string.Format("Tunynet_ShowModalDialog_ClosePage_{0}", "");
            if (!page.ClientScript.IsStartupScriptRegistered(scriptKey))
            {
                page.ClientScript.RegisterStartupScript(page.GetType(), scriptKey, GetCloseScript(""));
            }
        }


        private static string GetCloseScript(string returnValue)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<script language=\"javascript\" type=\"text/javascript\">");
            sb.AppendLine("<!--");
            sb.AppendLine("function CloseWindow(){");
            if (returnValue != string.Empty)
            {
                sb.AppendFormat("window.returnValue={0};",returnValue);
                sb.AppendLine();
            }
            sb.AppendLine("window.close();");
            sb.AppendLine("}");
            sb.AppendLine("CloseWindow();");
            sb.AppendLine("//-->");
            sb.AppendLine("</script>");

            return sb.ToString();
        }
    }
}